package com.example.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.model.dtos.PageResult;
import com.example.model.dtos.R;
import com.example.model.pojo.Supplier;
import com.example.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 基础信息控制类
 */
@RestController
@RequestMapping("basic/supplier/")
public class SupplierController {

    @Autowired(required = false)
    SupplierService supplierService;

    /**
     * 添加供应商
     */
    @RequestMapping("addSupplier")
    public R<String> addSupplier(@RequestBody Map<String,String> map){
        String name = map.get("name");
        String address = map.get("address");
        String postcode = map.get("postcode");
        String phone = map.get("phone");
        String fax = map.get("fax");
        String bank = map.get("bank");
        String bankNumber = map.get("bankNumber");
        String remark = map.get("remark");

        //判断参数是否为空
        if (StringUtils.isBlank(name)){
            return R.error("添加失败");
        }
        Supplier supplier = new Supplier(null,name,address,postcode,phone,fax,bank,bankNumber,remark);

        Boolean flag = supplierService.addSupplier(supplier);
        if (!flag){
            return R.error("添加失败");
        }

        return R.success("添加成功");
    }

    /**
     * 删除供应商
     */
    @RequestMapping("deleteSupplier")
    public R<String> deleteSupplier(@RequestBody Map<String,String> map){
        String id = map.get("myId");
        //判断参数是否为空
        if (StringUtils.isBlank(id)){
            return R.error("参数错误");
        }

        Boolean flag = supplierService.deleteSupplier(id);
        if (!flag){
            return R.error("删除失败");
        }

        return R.success("删除成功");
    }

    /**
     * 修改供应商信息
     */
    @RequestMapping("updateSupplier")
    public R<String> updateSupplier(@RequestBody Map<String,String> map){
        String id = map.get("myId");
        //判断参数是否为空
        if (StringUtils.isBlank(id)){
            return R.error("参数错误");
        }
        String name = map.get("name");
        String address = map.get("address");
        String postcode = map.get("postcode");
        String phone = map.get("phone");
        String fax = map.get("fax");
        String bank = map.get("bank");
        String bankNumber = map.get("bankNumber");
        String remark = map.get("remark");
        Supplier supplier = new Supplier(id,name,address,postcode,phone,fax,bank,bankNumber,remark);




        Boolean flag = supplierService.updateSupplier(supplier);
        if (!flag){
            return R.error("更新成功");
        }

        return R.success("更新成功");
    }

    /**
     * 查看供应商列表
     */
    @RequestMapping("SupplierList")
    public R<Object> SupplierList(@RequestBody Map<String,String> map){
        String pageStr = map.get("current");
        String pageSizeStr = map.get("size");
//        若为空，则查询第一页，每页10条
        if (StringUtils.isBlank(pageStr)){
            pageStr="1";
        }
        if (StringUtils.isBlank(pageSizeStr)){
            pageSizeStr="10";
        }
        Integer page = Integer.parseInt(pageStr);
        Integer pageSize = Integer.parseInt(pageSizeStr);


        PageResult pageResult = supplierService.SupplierList(page,pageSize);
        return R.success(pageResult);
    }
}
